package mx.org.banxico.sisal.dao;

import cve.entidades.CVE;
import cve.parser.CVEParser;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import mx.org.banxico.sisal.db.ConnectionFactory;

public class VulnerabilityDAO implements java.io.Serializable {

    private static final Logger LOG = Logger.getLogger(VulnerabilityDAO.class.getName());
    private static final long serialVersionUID = -1L;
    Connection connection;
    Statement stmt;
    private int noOfrecord;

    public VulnerabilityDAO() {
        prepararLista();
    }

    private static Connection getConnection() {
        Connection con = ConnectionFactory.getInstance().getConnection();
        return con;
    }

    public List<CVE> retrieveAllCVEs(int offset, int noOfRecords) {
        String query = "SELECT * FROM DB LIMIT " + offset + ", " + noOfRecords;
        List<CVE> cvelist = new ArrayList<>();
        CVE ncve = null;
        try {
            connection = getConnection();
            stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {
                ncve = new CVE(rs.getString("name"));
                ncve.setDescription(rs.getString("description"));
                //TODO: Agregar los restantes
                cvelist.add(ncve);
            }
            rs.close();
            rs = stmt.executeQuery("SELECT FOUND_ROWS()");
            if (rs.next()) {
                this.noOfrecord = rs.getInt(1);
            }
        } catch (SQLException e) {
            LOG.log(Level.INFO, "Ocurrio una excepci\u00f3n SQL: {0}", e.getMessage());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                LOG.log(Level.INFO, "Ocurrio una excepci\u00f3n SQL: {0}", e.getMessage());
            }
        }
        return cvelist;
    }

    public int getNoOfrecord() {
        return noOfrecord;
    }

    public List<CVE> retrieveAllCVEsFromFile() {
        CVEParser mCVEParser = new CVEParser();
        mCVEParser.setFiltro("");
        List<CVE> cveList = mCVEParser.getListCVE(getClass().getResourceAsStream("/resources/nvdcve-recent.xml"));
        if (cveList.isEmpty()) {
            return new ArrayList<>();
        }
        return cveList;
    }

    private List<CVE> listaCVE;

    private void prepararLista() {
        CVEParser mCVEParser = new CVEParser();
        mCVEParser.setFiltro("");
        listaCVE = mCVEParser.getListCVE(getClass().getResourceAsStream("/resources/nvdcve-recent.xml"));
        if (listaCVE.isEmpty()) {
            listaCVE = new ArrayList<>();
        }
        this.noOfrecord = listaCVE.size();
    }

    public List<CVE> retrieveFromList(int offset, int noOfRecords) {
        LOG.log(Level.INFO, "Par\u00e1metros recibidos: {0} y {1}", new Object[]{offset, noOfRecords});
        List<CVE> temp = new ArrayList<>();
        CVE nCVE;
        for (int i = offset; i < offset + noOfRecords; i++) {
            if (i >= this.noOfrecord) {
                break;
            }
            nCVE = listaCVE.get(i);
            LOG.log(Level.INFO, "Agregando: {0}", nCVE.getName());
            temp.add(nCVE);
        }
        return temp;
    }

}
